<?php
require_once "db.php";

$cinema = db_escape($_POST['add_proj_cin']);

if ($cinema==="choose_cinema") {
	echo "Choose cinema!";
	exit();
}

$hall = db_escape($_POST['add_proj_hall']);
$movie = db_escape($_POST['add_proj_mov']);
$ondate = db_escape($_POST['add_proj_date']);
$ontime = db_escape($_POST['add_proj_time']);
date_default_timezone_set('Europe/Sofia');

$current_date = date("Y/m/d");
$current_time = date("H:i:s");
$next_begin = 9373489940;

function time_date($s_time, $c_date, $s_date){
	return (strtotime($s_time)-strtotime($c_date))+strtotime($s_date);
}



$take_cinemaid = "SELECT * FROM cinema WHERE cinema_name = '$cinema'";
$get_cinemaid = db_query($take_cinemaid);
while ($cinemaid = mysqli_fetch_assoc($get_cinemaid)) {
		$cinema_id = $cinemaid['idcinema'];
	}
$take_movie = "SELECT idmovies, duration FROM movies WHERE movie_name ='$movie'";
	$get_movie = db_query($take_movie);
	while ($row = mysqli_fetch_assoc($get_movie)) {
		$movie_id = $row['idmovies']; 
		$duration = $row['duration']*60;
		$proj_end = date("H:i:s", strtotime($ontime)+$duration);
	}
$take_hallid = "SELECT idhalls FROM halls WHERE cinema_idcinema = '$cinema_id' AND hall_number = '$hall'";
$get_hallid = db_query($take_hallid);
while ($hallid = mysqli_fetch_assoc($get_hallid)) {
		$hall_id = $hallid['idhalls'];
	}

if (time_date($ontime, $current_date, $ondate)<strtotime($current_time)) {
	echo 'Try again. The time that you set is in the past.';
	exit();
}
else {
	$beg_end = "SELECT proj_begin, proj_end FROM projection 
				WHERE cinema_idcinema='$cinema_id' AND halls_idhalls='$hall_id' AND proj_date='$ondate'";
	$beg_end_query = db_query($beg_end);
	while($matches = mysqli_fetch_assoc($beg_end_query)){
		if (((time_date($ontime, $current_date, $ondate)>time_date($matches['proj_begin'], $current_date, $ondate))
			&&(time_date($ontime, $current_date, $ondate)<time_date($matches['proj_end'], $current_date, $ondate)))||
			((time_date($proj_end, $current_date, $ondate)>time_date($matches['proj_begin'], $current_date, $ondate))
			&&(time_date($proj_end, $current_date, $ondate)<time_date($matches['proj_end'], $current_date, $ondate)))) {
			echo 'Try again. Your interval dublicates another that already exist or there is not enough time';
			exit();
		}
		else {
			if (time_date($proj_end, $current_date, $ondate) < time_date($matches['proj_begin'], $current_date, $ondate)) {
				if ($next_begin > time_date($matches['proj_begin'], $current_date, $ondate)) {
					$next_begin = time_date($matches['proj_begin'], $current_date, $ondate);
				}
			}
		}
	}
	if (($next_begin-time_date($ontime, $current_date, $ondate))<$duration) {
		echo 'Try again. The time between your and next movie is not enough.';
		exit();
	}
	else {
		$query = "INSERT INTO projection (proj_date, proj_begin, proj_end, movies_idmovies, halls_idhalls, cinema_idcinema) 
		VALUES ('$ondate', '$ontime', '$proj_end', '$movie_id', '$hall_id', '$cinema_id')";
		$result = db_query($query);
		$cin_has_mov_query = "INSERT IGNORE INTO cinema_has_movies (cinema_idcinema, movies_idmovies) 
		VALUES ('$cinema_id', '$movie_id')";
		$cin_has_mov = db_query($cin_has_mov_query);
		echo 'You have added a new projection for '.$movie.' on '.$ondate.' at '.$ontime.' in '.$cinema;
	}
}

